<?php
if ( !function_exists( 'fileExistsAtUrl' ) ) {
	function fileExistsAtUrl($url) {

		$data = makeHeaderRequest($url);
		
		if(!$data) {
			
			return false;
			
		} else {
			
			preg_match_all('/HTTP\/1\.[1|0]\s(\d{3})/',$data,$matches);
			$code = end($matches[1]);
			
			if ($code == '200') {
				
				return true;
			  
			} else if ($code == '501') {
				
				return true;
			
			} elseif ($code == '404') {
				
				return false;
			  
			}
			
			
		}
		
	}
	
}
if ( !function_exists( 'makeHeaderRequest' ) ) {
	function makeHeaderRequest($url) {
			
			$ch = curl_init();
			
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, true);
			curl_setopt($ch, CURLOPT_NOBODY, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
			//Limit the size of the data returned, we dont want the script going through the whole page when it checks, as well as when it parses.
			curl_setopt($ch, CURLOPT_RANGE,"1-2"); 
			curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //only follow 2 redirects.
			
			$data = curl_exec($ch);
			curl_close($ch);
			
			return $data;
			
	}
}

if ( !function_exists( 'curlGetFile' ) ) {
	function curlGetFile($url)
	{
			$curl_handle=curl_init();
			curl_setopt($curl_handle,CURLOPT_URL,$url);
			curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
			$data = curl_exec($curl_handle);
			curl_close($curl_handle);
			
			return $data;
	}
}